package com.squid.search.controller;

import co.elastic.clients.elasticsearch.indices.GetIndexResponse;
import com.alibaba.fastjson.JSON;
import com.squid.base.common.bean.Return;
import com.squid.search.model.ArticleInfo;
import com.squid.search.service.document.DocumentService;
import com.squid.search.service.index.EsIndexService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.ArrayList;

/**
 * Description
 *
 * @author jinhang
 * @since 2024-2-21
 **/
@RestController
@RequestMapping("search")
public class EsController {
    @Resource
    EsIndexService esIndexService;
    @Resource
    DocumentService documentService;

    @PostMapping("insertDo")
    public Return<Object> insertDo(@RequestBody ArticleInfo articleInfo) {
        esIndexService.createIndex(articleInfo.getAuthorId());
        return Return.success();
    }

    @PostMapping("getIndex")
    public Return<Object> getIndex(@RequestBody ArticleInfo articleInfo) {
        GetIndexResponse indexDetail = esIndexService.getIndexDetail(articleInfo.getAuthorId());
        return Return.success().data(indexDetail.result());
    }

    @PostMapping("updateDo")
    public void updateDo(@RequestBody ArticleInfo articleInfo) {
//        esIndexService.updateIndexProperty();
    }

    @PostMapping("deleteDo")
    public void deleteDo(){

    }

    @PostMapping("searchDo")
    public Return<Object> searchDo() throws IOException {
        GetIndexResponse indexList = esIndexService.getIndexList();
        System.out.println("*****");
        indexList.result().forEach((x, y) -> {
            System.out.println(x);
            System.out.println(y.mappings().properties());
        });
        System.out.println("*****");
        String json = "{\n" +
                "  \"id\": \"id_92032f196f9c\",\n" +
                "  \"authorId\": \"authorId_99b94dd09b47\",\n" +
                "  \"title\": \"title_1b5a4355e4cf\",\n" +
                "  \"content\": \"content_3d96c480bf75\",\n" +
                "  \"category\": \"category_d56590200e28\",\n" +
                "  \"tags\": [\n" +
                "    \"tags_10f64d9d238b\"\n" +
                "  ],\n" +
                "  \"commonsId\": \"commonsId_d8bf75c0bf72\",\n" +
                "  \"likesId\": \"likesId_31696d9c37d8\",\n" +
                "  \"status\": \"status_35910a0a9100\",\n" +
                "  \"createTime\": \"2024-03-12 15:05:24\",\n" +
                "  \"updateTime\": \"2024-03-12 15:05:24\",\n" +
                "  \"deleteToken\": 0,\n" +
                "  \"enableToken\": 0\n" +
                "}";
        ArticleInfo javaObject = JSON.parseObject(json, ArticleInfo.class);
        ArticleInfo javaObject2 = JSON.parseObject(json, ArticleInfo.class);
        ArrayList<ArticleInfo> articleInfos = new ArrayList<>();
        articleInfos.add(javaObject);
        articleInfos.add(javaObject2);
        return Return.success().data(javaObject);
    }
}
